//------------------------------------------------------------------------------
// X2D Game Engine
// Copyright (C) 2006-2007 Janusz Kowalski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

#include <cmath>
#include <cassert>

#include "imgSDL.h"
#include "fontSDL.h"

void Font::load(const char *path) {
  delete fontImg_; chW_=0; chH_=0;
  fontImg_ = new Image(path);
  chW_=fontImg_->getW()/16;
  chH_=fontImg_->getH()/16;
  //getImageWH(path, chW_, chH_);
  //chW_/=16; chH_/=16;
}

Font::Font() {
  fontImg_=0;
  effect=FONT_EFFECT_NONE;
}

Font::Font(const char *path) {
  effect=FONT_EFFECT_NONE;
  fontImg_=0; load(path);
}

Font::Font(const Font &src) {
  effect = src.effect; chW_=src.chW_; chH_=src.chH_;
  fontImg_ = new Image(*src.fontImg_);
}

Font &Font::operator=(const Font &right) {
  if(&right!=this) {
    delete fontImg_;
    fontImg_ = new Image(*right.fontImg_);
    effect = right.effect; chW_=right.chW_; chH_=right.chH_;
  }
  return *this;
}

Font::~Font() {
  delete fontImg_;
}

void Font::draw(GraphicContext &g, int x, int y, unsigned char c) {
  SDL_Rect src_rect;
  src_rect.x=(c%16)*chW_;
  src_rect.y=(c/16)*chH_;
  src_rect.w=chW_;
  src_rect.h=chH_;
  assert(effect==FONT_EFFECT_NONE || effect==FONT_EFFECT_WAVES);
  if((effect & FONT_EFFECT_WAVES)>0) {
    int n=(SDL_GetTicks())%256;
    y+=(int)ceil(4*sin(3.1415*(double)(x+SDL_GetTicks()/2)/(double)(16*9)));
  }
  g.blitImage(*fontImg_, &src_rect, x, y);
}


void Font::drawText(GraphicContext &g, int x, int y, const char *text) {
  for(int i=0; text[i]!=0; i++) draw(g, x+chW_*i, y, text[i]);
}

void Font::drawText(GraphicContext &g, int x, int y, const char *text, int n) {
  for(int i=0; i<n; i++) draw(g, x+chW_*i, y, text[i]);
}

void Font::drawText(SDL_Surface *dest_surf, int x, int y, const char *text) {
  GraphicContext g = GraphicContext(dest_surf);
  drawText(g,x,y,text);
}

void Font::drawText(SDL_Surface *dest_surf, int y, const char *text) {
  int x=(dest_surf->w-w(text))/2;
  drawText(dest_surf, x, y, text);
}

#define ROW_SPACE 2

void Font::draw_text_centered(SDL_Surface *dest_surf, const char *text) {
  int h=chH_+ROW_SPACE;
  char *buffer;
  for(int i=0; text[i]!=0; i++) if(text[i]=='\n') h+=chH_+ROW_SPACE;
  buffer=new char[256];
  int i=0, j=0, line=0;
  while(1) {
    if(text[i]==0 || text[i]=='\n') {
      buffer[j]=0;
      drawText(dest_surf, (dest_surf->w-chW_*j)/2, (dest_surf->h-h)/2+(chH_+ROW_SPACE)*line+2, buffer);
      line++;
      j=0;
    } else {
      buffer[j]=text[i];
      j++;
    }
    if(text[i]==0) break;
    i++;
  };
  delete buffer;
}

int Font::getW(const char *text) {
  int i;
  for(i=0; text[i]!=0; i++);
  return i*chW_;
}

int Font::getH(const char *text) {
  return chH_;
}

int Font::w(const char *text) {
  int i;
  for(i=0; text[i]!=0; i++);
  return i*chW_;
}

int Font::h(const char *text) {
  return chH_;
}
